package cn.vivi.tag.controller;

import cn.vivi.common.entity.Tag;
import cn.vivi.common.util.Result;
import cn.vivi.tag.service.TagService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("tags")
public class TagController {

    @Autowired
    private TagService tagService;

    @GetMapping("blog/{id}")
    public Result<List<Tag>> findByBlogId(@PathVariable String id) {
        return Result.success(tagService.findByBlogId(id));
    }


    @PostMapping
    public Result addTag(@RequestBody Tag tag) {
        return tagService.addTag(tag) ? Result.success(): Result.error("添加标签失败");
    }

    @GetMapping("/{current}/{size}")
    public Result<Page<Tag>> findTagsByPage(@PathVariable long current, @PathVariable long size) {
        return Result.success(tagService.findByPage(current, size));
    }

    @PutMapping
    public Result updateTag(@RequestBody Tag tag) {
        return tagService.updateTag(tag) ? Result.success(): Result.error("更新标签失败");
    }

    @DeleteMapping("{id}")
    public Result deleteTag(@PathVariable String id) {
        return tagService.deleteTag(id) ? Result.success(): Result.error("删除标签失败");
    }

}
